package net.idik.lib.cipher.so.encrypt;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESEncryptor {

    private static final String AES_MODE = "AES/CBC/PKCS5Padding";
    private static final String CHARSET = "UTF-8";
    private static final String HASH_ALGORITHM = "MD5";
    private static byte[] iv;

    static {
        AESEncryptor.iv = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    }

    private AESEncryptor() throws IllegalAccessException {
        throw new IllegalAccessException();
    }

    public static String decrypt(String arg2, String arg3) {
        try {
            return new String(AESEncryptor.decrypt(AESEncryptor.generateKeySpec(arg2), AESEncryptor.iv, Base64.decode(arg3, 0)), "UTF-8");
        }
        catch(NoSuchAlgorithmException v2_6) {
            v2_6.printStackTrace();
            return "";
        }
        catch(UnsupportedEncodingException v2_5) {
            v2_5.printStackTrace();
            return "";
        }
        catch(NoSuchPaddingException v2_4) {
            v2_4.printStackTrace();
            return "";
        }
        catch(InvalidKeyException v2_3) {
            v2_3.printStackTrace();
            return "";
        }
        catch(InvalidAlgorithmParameterException v2_2) {
            v2_2.printStackTrace();
            return "";
        }
        catch(IllegalBlockSizeException v2_1) {
            v2_1.printStackTrace();
            return "";
        }
        catch(BadPaddingException v2) {
            v2.printStackTrace();
            return "";
        }
    }

    private static byte[] decrypt(SecretKeySpec arg2, byte[] arg3, byte[] arg4) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher v0 = Cipher.getInstance("AES/CBC/PKCS5Padding");
        v0.init(2, arg2, new IvParameterSpec(arg3));
        return v0.doFinal(arg4);
    }

    public static String encrypt(String arg2, String arg3) {
        try {
            byte[] v2_7 = AESEncryptor.encrypt(AESEncryptor.generateKeySpec(arg2), AESEncryptor.iv, arg3.getBytes("UTF-8"));
            return v2_7 == null ? "" : Base64.encodeToString(v2_7, 0);
        }
        catch(NoSuchAlgorithmException v2_6) {
            v2_6.printStackTrace();
            return "";
        }
        catch(UnsupportedEncodingException v2_5) {
            v2_5.printStackTrace();
            return "";
        }
        catch(NoSuchPaddingException v2_4) {
            v2_4.printStackTrace();
            return "";
        }
        catch(InvalidKeyException v2_3) {
            v2_3.printStackTrace();
            return "";
        }
        catch(InvalidAlgorithmParameterException v2_2) {
            v2_2.printStackTrace();
            return "";
        }
        catch(IllegalBlockSizeException v2_1) {
            v2_1.printStackTrace();
            return "";
        }
        catch(BadPaddingException v2) {
            v2.printStackTrace();
            return "";
        }
    }

    private static byte[] encrypt(SecretKeySpec arg2, byte[] arg3, byte[] arg4) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher v0 = Cipher.getInstance("AES/CBC/PKCS5Padding");
        v0.init(1, arg2, new IvParameterSpec(arg3));
        return v0.doFinal(arg4);
    }

    private static SecretKeySpec generateKeySpec(String arg2) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest v0 = MessageDigest.getInstance("MD5");
        v0.update(arg2.getBytes("UTF-8"));
        return new SecretKeySpec(v0.digest(), "AES");
    }

    public static void setIv(String arg1) {
        try {
            AESEncryptor.iv = arg1.getBytes("UTF-8");
        }
        catch(UnsupportedEncodingException v1) {
            v1.printStackTrace();
        }
    }

}
